package com.zx.ce.ws;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.webservice.SoapClient;
import cn.hutool.json.JSONUtil;
import com.zx.ce.utils.HttpUtils;
import org.apache.cxf.service.model.ServiceInfo;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;

import javax.xml.xpath.XPathConstants;
import java.util.*;

/**
 * 测试ws连接
 *
 * @author 金聖聰
 * @version v1.0
 * @email jinshengcong@163.com
 * @since Created in 2021/10/21 23:35
 */
public class TestWsConnect {
    public static void main(String[] args) {


        ServiceInfo service = new ServiceInfo();
        // 新建客户端
        SoapClient client = SoapClient.create("http://localhost:8080/webservice/loginApi?wsdl")
                // SoapClient client = SoapClient.create("http://服务器端口地址/webservice/loginApi?wsdl")
                // 设置要请求的方法，此接口方法前缀为web，传入对应的命名空间
                .setMethod("jin:userLogin", "http://jinsc.com")
                // 设置参数，此处自动添加方法的前缀：jin
                // false的意思就是没有前缀
                .setParam("name", "jinshengcong3080ti", false);


        // 发送请求，参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
        String send = client.send(true);
        Console.log(send);
        // 把send返回的xml内容转换成document对象
        Document document = XmlUtil.parseXml(send);
        List<String> nameList = Arrays.asList("errMsg", "errorCode", "name");
        HashMap<String, String> resultMap = new HashMap<>();
        nameList.forEach(e -> {
            // 循环上面的nameList列表,每个元素都是return后面的标签,得标签里面的值
            String result = (String) XmlUtil.getByXPath("//return/" + e, document, XPathConstants.STRING);
            // 然后给结果map赋值,key是name,value是标签中的值
            resultMap.put(e, result);
        });
        // 得到结果map
        Console.log(resultMap);
        // map转Dto,其中属性要一致
        UserLoginDto convert = Convert.convert(UserLoginDto.class, resultMap);
        Console.log(convert);

    }
    
    @Test
    public void test(){
        HashMap<String, String> headMap = new HashMap<>(2);
        headMap.put("Content-Type","text/xml;charset=UTF-8");
        headMap.put("SOAPAction","");

        // String xmlBody = ResourceUtil.readUtf8Str("1.xml");
        String xmlBody ="<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:log=\"http://login.webservice.bos.kingdee.com\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <log:login soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                "         <userName xsi:type=\"xsd:string\">user</userName>\n" +
                "         <password xsi:type=\"xsd:string\">wsl123</password>\n" +
                "         <slnName xsi:type=\"xsd:string\">eas</slnName>\n" +
                "         <dcName xsi:type=\"xsd:string\">wensli0318</dcName>\n" +
                "         <language xsi:type=\"xsd:string\">l2</language>\n" +
                "         <dbType xsi:type=\"xsd:int\">2</dbType>\n" +
                "      </log:login>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        String put = HttpUtils.doPostForString("http://183.129.165.54:188", "/ormrpc/services/EASLogin?wsdl"
                , headMap, Collections.emptyMap(), xmlBody);
        Map<String, Object> xml = XmlUtil.xmlToMap(put);
        
        System.out.println("结果："+put);
    }
    @Test
    public void test2(){
        ArrayList<List<String>> list = new ArrayList<>();
        ArrayList<String> ele = new ArrayList<>();
        ele.add("038000"); // companyNumber
        ele.add("v001"); //voucherNumber
        ele.add(""); // importKey
        ele.add("2020"); //periodYear
        ele.add("2"); //periodNumber
        ele.add("2020-02-01"); //bookedDate
        ele.add("2020-03-01"); //bizDate
        ele.add("记"); //voucherType
        ele.add("038000"); //
        ele.add("038000"); //
        list.add(ele);
        System.out.println(JSONUtil.toJsonStr(list));
    }


    @Test
    public void test33(){
        HashMap<String, String> headMap = new HashMap<>(2);
        headMap.put("Content-Type","text/xml;charset=UTF-8");
        headMap.put("SOAPAction","");

        String xmlBody ="<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <Body>\n" +
                "        <deleteVoucherByID xmlns=\"http://183.129.165.54:188/ormrpc/services/WSGLWebServiceFacade\">\n" +
                "            <companyNumber>038000</companyNumber>\n" +
                "            <voucherID>bopRVqBBRommV89E4d5vrSZS4B4</voucherID>\n" +
                "        </deleteVoucherByID>\n" +
                "    </Body>\n" +
                "</Envelope>";
        String put = HttpUtils.doPostForString("http://183.129.165.54:188", "/ormrpc/services/WSGLWebServiceFacade?wsdl"
                , headMap, Collections.emptyMap(), xmlBody);
        Map<String, Object> xml = XmlUtil.xmlToMap(put);

        System.out.println("结果："+put);
    }


    @Test
    public void test34(){
        HashMap<String, String> headMap = new HashMap<>(2);
        headMap.put("Content-Type","text/xml;charset=UTF-8");
        headMap.put("SOAPAction","");

        String xmlBody ="<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <Body>\n" +
                "        <findVoucher xmlns=\"http://183.129.165.54:188/ormrpc/services/WSGLWebServiceFacade\">\n" +
                "            <comNumber>038000</comNumber>\n" +
                "            <year>2021</year>\n" +
                "            <periodNnumber>11</periodNnumber>\n" +
                "        </findVoucher>\n" +
                "    </Body>\n" +
                "</Envelope>";
        String put = HttpUtils.doPostForString("http://183.129.165.54:188", "/ormrpc/services/WSGLWebServiceFacade?wsdl"
                , headMap, Collections.emptyMap(), xmlBody);
        Map<String, Map> xml =(Map<String, Map>)(Object) XmlUtil.xmlToMap(put);
        HashMap<String,Object> o = (HashMap<String,Object>)xml.get("soapenv:Body").get("ns1:findVoucherResponse");
        Map a = (Map) o.get("findVoucherReturn");
        Object aReturn = a.get("findVoucherReturn");
        System.out.println(aReturn);

    }
    
    @Test
    public void test22(){
        ArrayList<String> list = new ArrayList<>(1);
        list.add("111");

        String s = list.get(1);
        System.out.println(s);
    }
    @Test
    public void rw(){
        for (int i = 0; i < 5; i++) {
            test34();
        }
    }
}
